package xj.graph2d.app.prop;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

import xj.graph2d.app.resources.Style;

public class TextFieldDialog extends ModelPropertyField {

  TextFieldDialog(String fieldName, String displayName) {
    this(fieldName, displayName, displayName);
  }

  TextFieldDialog(String fieldName, String displayName, String dialogTitle) {
    super(fieldName, displayName);
    JButton editButton = makeEditButton("Edit");

    textDialog = new TextDialog(null, dialogTitle, this);
    editButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	updateEditorValue();
	textDialog.showDialog();
      }
    });
    setEditor(editButton);
  }

  @Override
  public void updateEditorValue() {
    Object v = getModelValue();
    if (v == null) {
      textDialog.setText(null);
    } else if (v instanceof String) {
      textDialog.setText((String) v);
    }
  }

  @Override
  public void updateModelValue() {
    String s = textDialog.getText();
    setModelValue(s);
  }

  protected TextDialog textDialog;

}

class TextDialog extends JDialog implements ActionListener {

  public TextDialog(JFrame owner, String title, TextFieldDialog field) {
    super(owner, title, true);
    setAlwaysOnTop(true);

    this.field = field;

    getContentPane().setLayout(new BorderLayout());
    JPanel topPanel = new JPanel();
    topPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    JPanel textPanel = new JPanel();
    textPanel.setBackground(Color.white);
    textPanel.setLayout(new BorderLayout());
    textEditor = new JTextArea();
    textEditor.setFont(Style.font);
    textEditor.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    textPanel.add(textEditor, BorderLayout.CENTER);
    topPanel.add(textPanel, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);
    buttonPanel.add(cancelButton);
    okButton = new JButton("Ok");
    okButton.addActionListener(this);
    buttonPanel.add(okButton);
    topPanel.add(buttonPanel, BorderLayout.SOUTH);

    setSize(400, 250);
  }

  public void showDialog() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dialogSize = getSize();
    setLocation(screenSize.width / 2 - dialogSize.width / 2, screenSize.height
	/ 2 - dialogSize.height / 2);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == okButton) {
      if (field != null) {
	field.updateModelValue();
	field.updateShape();
      }
      setVisible(false);
    } else {
      // cancel
      setVisible(false);
    }
  }

  public String getText() {
    if (textEditor != null) {
      return textEditor.getText();
    }
    return null;
  }

  public void setText(String s) {
    if (textEditor != null) {
      textEditor.setText(s);
    }
  }

  protected TextFieldDialog field;

  protected JButton okButton, cancelButton;

  protected JTextArea textEditor;

}
